// pages/home/home.ts
// <wxs src="../../utils/filter.wxs" module="filter" />
Page({

  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },

  data: {
    //轮播图片
    background: [{}],
    //轮播属性
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    circular: true,
    interval: 2000,
    duration: 500,
    //首页展示图片
    showimg: [{}],
    url: '',
    //触底标志
    again: false,
    //页数
    page:1
  },
  changeProperty: function (e: any) {
    var propertyName: any = e.currentTarget.dataset.propertyName;
    console.log(propertyName)
    var newData: any
    newData[propertyName] = e.detail.value
    this.setData(newData)
  },
  changeIndicatorDots: function () {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function () {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e: any) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e: any) {
    this.setData({
      duration: e.detail.value
    })
  },
  search() {
    console.log(5252)
    wx.navigateTo({
      url: '/pages/search/search'
    })
  },
  /**
   * 页面的初始数据
   *

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad() {
    var app = getApp();
    const that = this;
    //广告
    wx.showLoading({
      title: '小的正在努力！',
    })
    this.setData({
      // url: 'http://192.168.3.190:7001/'
      url: app.globalData.url
    })
    // console.log(app.globalData.url)
    //新闻
    wx.request({
      url: `${this.data.url}api/news`, //仅为示例，并非真实的接口地址
      // url: `${app.globalData.url}api/news`, //仅为示例，并非真实的接口地址
      success(res: { data: { code: 0, data: { data: { id: number, news_image: string, news_name: string }[] }[] } }) {
        console.log(res);
        const info = res.data.data;
        wx.hideLoading()
        console.log(info)
        // that.data.background=info
        that.setData({
          background: info,

        })
        app.globalData.history = info
        console.log(app.globalData.history)


      }
    })
    this.article(this.data.page);


  },
  //分类跳转
  go(event: any) {
    console.log(event.currentTarget.dataset.value)
    wx.navigateTo({
      url: `/pages/search_after/search_after?keyname=${event.currentTarget.dataset.value}`
    })

  },
  go_com(event: any) {
    console.log(event.currentTarget.dataset.id)
    wx.navigateTo({
      url: `/pages/goods/goods?id=${event.currentTarget.dataset.id}`
    })

  },
  //获取文章
  article(page:number) {
    //文章
    const that = this
    wx.request({
      url: `${this.data.url}api/product?page=${page}`, //仅为示例，并非真实的接口地址
      success(res: { data: { code: 0, data: { data: { id: number, news_image: string, news_name: string }[] }[] } }) {
        console.log(res);
        const info = res.data.data;
        console.log(info)
        // that.data.background=info
        console.log(that.data.showimg[0])
        if(that.data.page===1){
          console.log(1111)
          that.setData({
            showimg: info
          })
        }else{
          that.setData({
            showimg: that.data.showimg.concat(info)
          })
        }
        
        console.log(that.data.showimg)
        if(info.length<5){
          that.setData({
            again: true
          })
        }

      }
    })
    console.log(that.data.background)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  look(event: any) {
    const value = event.currentTarget.dataset.name
    wx.navigateTo({
      url: `/pages/search_after/search_after?keyname=${value}`
    })
  },
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    this.article(1);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      page:this.data.page+1
     })
    this.article(this.data.page)
    console.log(1111)
    console.log(this.data.page)
    
  },

  /**
   * 用户点击右上角分享
   */
  /*  onShareAppMessage() {

   } */
})